package com.lzq.framework.util.time;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author 2018/12/19 16:48 by 刘赵强
 **/
public class DateTimeUtil {
    public static final String ISO_BASE_DATE = "yyyyMMdd";
    public static final String ISO_BASE_DATETIME = "yyyyMMddHHmmss";
    public static final String ISO_BASE_DATETIME_MILLI = "yyyyMMddHHmmssSSS";
    public static final String ISO_DASH_DATE = "yyyy-MM-dd";
    public static final String ISO_DASH_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String ISO_DASH_DATETIME_MILLI = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String ISO_SLASH_DATE = "yyyy/MM/dd";
    public static final String ISO_SLASH_DATETIME = "yyyy/MM/dd HH:mm:ss";
    public static final String ISO_SLASH_DATETIME_MILLI = "yyyy/MM/dd HH:mm:ss.SSS";

    private DateTimeUtil() {
    }

    /**
     * 时间格式化
     *
     * @param date    日期
     * @param pattern 日志格式
     * @return 格式化后的日志字符串
     * @date 2018/12/19 17点00分
     * @author 刘赵强
     */
    public static String format(Date date, String pattern) {
        return DateFormatUtils.format(date, pattern);
    }


    public static Date getNextSunday() {
        LocalDate with = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        return localDate2Date(with);
    }

    private static Date localDate2Date(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }
}
